-- UIAwakeRefreshProp
-- Create by chendh
-- 觉醒雕像属性重铸界面

--UIAwakeRefreshProp继承自Layer
UIAwakeRefreshProp = class("UIAwakeRefreshProp", function()
    return cc.Layer:create();
end);


function UIAwakeRefreshProp.create(petId)
    return UIAwakeRefreshProp.new(petId);
end

-- 内部函数声明
local resize;

-- 构造函数
function UIAwakeRefreshProp:ctor(petId)
    -- 初始化
    local node = cc.CSLoader:createNode("layout/awake/AwakeRefreshProp.csb");
    self:addChild(node);
    self:setName("UIAwakeRefreshProp");

    self.node = node;
    self.petId = petId;

    -- 注册事件处理回调函数
    self:registerEventCallback()

    -- 注册点击事件
    self:registerTouchEvent();

    -- 重绘
    self:redraw();

    -- 适配
    resize(node);
end

-- 重绘
function UIAwakeRefreshProp:redraw()
    local titleLabel = findChildByName(self.node, "CT/template/title");
    local subheadLabel = findChildByName(self.node, "CT/template/subhead");
    local descLabel = findChildByName(self.node, "CT/desc");
    local propLabel = findChildByName(self.node, "CT/prop");
    local iconImg = findChildByName(self.node, "CT/carving_node/icon")

    iconImg:loadTexture(getAwakeIconPath("carved"));

    TextStyleM.setTitleStyle(titleLabel);
    TextStyleM.setSubheadStyle(subheadLabel);
    TextStyleM.setTextStyle(descLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_DARK_BROWN);
    TextStyleM.setTextStyle(propLabel, TextStyleM.TEXT_SIZE_SMALL, TextStyleM.TEXT_COLOR_BLUE, nil, 2);

    titleLabel:setString(string.format(getLocStr("awake_carving"), PetM.query(self.petId, "short_name")));
    subheadLabel:setString(getLocStr("awake_refresh_subhead"));

    local desc = "";
    local propStr = "";

    -- 冈布奥雕像的可重铸属性
    desc = "[blue]" .. AwakeM.fetchAwakeCarvingPropsDesc(self.petId)[2] .. "[-]\n";
    local chipCost = FormulaM.invoke("CALC_AWAKE_REFRESH_PROP_COST", 0);
    local gemCost = FormulaM.invoke("CALC_AWAKE_REFRESH_PROP_COST", 1);
    local chipCostStr = "";
    local gemCostStr = "";

    if ItemM.getAmount(ME.user, chipCost[2]) >= chipCost[3] then
        chipCostStr = "[blue]" .. chipCost[3] .. "[-]";
    else
        chipCostStr = "[red]" .. chipCost[3] .. "[-]";
    end

    if ME.user:queryAttrib("gem") >= gemCost[3] then
        gemCostStr = "[blue]" .. gemCost[3] .. "[-]";
    else
        gemCostStr = "[red]" .. gemCost[3] .. "[-]";
    end
    local costStr = string.format(getLocStr("awake_refresh_desc"), chipCostStr, gemCostStr);
    desc = desc .. costStr;
    propStr = getLocStr("awake_refresh_prop");

    descLabel = applyString(descLabel, desc);
    propLabel = applyString(propLabel, propStr);
end

-- 注册事件回调
function UIAwakeRefreshProp:registerEventCallback()
    -- 注册玩家信息更新的回调
    EventMgr.register("UIAwakeRefreshProp", event.AWAKE_REFRESH_PROP, function(args)
        if args.pet_id == self.petId then
            self:redraw();

            -- 设置已经刷新
            self.isRefreshing = nil;

            -- 播放光效
            local itemNode = findChildByName(self.node, "CT/carving_node/effect_node");
            itemNode:removeAllChildren();
            playEffect(itemNode, 1180, 30, -55,  nil, 0.82);

            -- 弹出提示语
            local prop = args.prop[1];
            local fieldName = FieldsM.getFieldName(prop[2]);
            alert(string.format(getLocStr("awake_refresh_succ"), fieldName, prop[3]));
        end

    end);

    -- 注册获得焦点的回调
    EventMgr.register("UIAwakeRefreshProp", event.FOCUS_CHANGED, function(para)
        if para["get"] == "UIAwakeRefreshProp" then
            UIMgr:getCurrentScene().topMenu:show(TOPMENU_FRAME_FIRST_RUINS_SCRAP, TOPMENU_FRAME_GEM);
        end
    end);

    -- 界面析构后，需要清理下
    self:registerScriptHandler(function(ev)
        if ev == "exit" then
            cclog("UIAwakeRefreshProp 界面析构清理");
            EventMgr.removeAll("UIAwakeRefreshProp");

        elseif ev == "enter" then

        end
    end);
end

-- 注册点击事件
function UIAwakeRefreshProp:registerTouchEvent()
    local bgNode = findChildByName(self.node,"bg/bgImg");
    bgNode:setTouchEnabled(true);
    local function onBgClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            AudioM.playFx("button_return");
            -- 点击关闭
            UIMgr.getCurrentScene():removeFormByName("UIAwakeRefreshProp");
        end
    end
    AddBackEventHandler(self, bgNode, onBgClick);

    -- 注册返回按钮点击事件
    local btnBack = findChildByName(self.node, "BT/btn_back");
    local function onBackClick(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            -- 关闭当前界面
            AudioM.playFx("button_return");
            UIMgr.getCurrentScene():removeFormByName("UIAwakeRefreshProp");
        end
    end
    AddBackEventHandler(self, btnBack, onBackClick);
    TextStyleM.setTextStyle(btnBack, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnBack:setTitleText(getLocStr("btn_text_back"));

    -- 重铸
    local btnOk1 = findChildByName(self.node, "CT/btn_ok1");
    local function onOk1Click(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            self:tryRefreshProp(0);
        end
    end
    btnOk1:addTouchEventListener(onOk1Click);
    TextStyleM.setTextStyle(btnOk1, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnOk1:setTitleText(getLocStr("btn_text_awake_refresh_prop"));

    -- 钻石重铸
    local btnOk2 = findChildByName(self.node, "CT/btn_ok2");
    local function onOk2Click(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            self:tryRefreshProp(1);
        end
    end
    btnOk2:addTouchEventListener(onOk2Click);
    TextStyleM.setTextStyle(btnOk2, TextStyleM.TEXT_SIZE_MIDDLE, TextStyleM.TEXT_COLOR_BROWN_YELLOW, true);
    btnOk2:setTitleText(getLocStr("btn_text_awake_refresh_prop_by_gem"));
end

-- 适配
function resize(node)
    -- bg居中显示并充满整个屏幕
    AlignM.fitToScreen(node, "bg");

    -- CT居中并缩放至合适大小
    AlignM.alignToCenter(node, "CT");

    -- BT置于底部并缩放至合适大小
    AlignM.alignToBottomCenter(node, "BT");
end

-- 尝试刷新属性
function UIAwakeRefreshProp:tryRefreshProp(style)
    -- 正在刷新，则不允许玩家连续点击
    if self.isRefreshing then
        return;
    end

    -- 判断是否足够消耗
    local cost = FormulaM.invoke("CALC_AWAKE_REFRESH_PROP_COST", style);

    if cost[1] == 2 then
        if cost[2] == "gem" then
            local curGem = ME.user:queryAttrib("gem");
            if curGem < cost[3] then
                -- 钻石不足提示
                showGemLackConfirm();
                return;
            end
        else
            alert(string.format(getLocStr("something_not_enough"), FieldsM.getFieldName(cost[2])));
            return;
        end
    else
        if cost[3] > ItemM.getAmount(ME.user, cost[2]) then
            alert(string.format(getLocStr("something_not_enough"), ItemM.query(cost[2], "name")));
            return;
        end
    end

    -- 刷新属性处理函数
    local function refreshProp()
        self.isRefreshing = true;
        Operation.cmd_awake_refresh_prop(self.petId, style);
    end

    -- 如果达到最大值，那么要弹出个提示
    if AwakeM.awakeCarvingPropIsMaxNum(self.petId) then
        confirm2(CONFIRM_TYPE_OK_CANCEL, getLocStr("slime_note"),
            string.format(getLocStr("awake_refresh_max_value_tip"), PetM.query(self.petId, "short_name")),
            refreshProp, nil, getLocStr("awake_refresh_max_value_ok"));
        return;
    end

    -- 刷新属性
    refreshProp();
end